<template>
  <div class="app-container">
    <el-card>
      <el-row :gutter="20" align="middle">
        <el-col :span="24" class="input-button-group">
          <el-select v-model="communityId" placeholder="选择小区名称" clearable>
            <el-option
              v-for="community in communityList"
              :key="community.id"
              :label="community.name"
              :value="community.id"
            />
          </el-select>
          <el-input v-model="content" placeholder="输入投诉内容" clearable style="width: 200px;" />
          <el-button icon="el-icon-search" @click="handleSearch">筛选</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      height="532"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" sortable :sort-by="sortById" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="小区名称" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.communityName }}
        </template>
      </el-table-column>

      <el-table-column label="投诉内容" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.content }}
        </template>
      </el-table-column>

      <el-table-column label="投诉人" width="180" align="center">
        <template slot-scope="scope">
          {{ scope.row.ownerName }}
        </template>
      </el-table-column>

      <el-table-column label="手机号" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>

      <!-- Actions Column -->
      <el-table-column label="操作" width="197" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block" style="text-align: center;">
      <el-pagination
        :current-page="page"
        :page-sizes="[9, 18, 27]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getList, deleteRow } from '@/api/complaint'
import { getCommunityList } from '@/api/community'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: 1,
      pageSize: 9,
      communityId: '',
      content: '',
      total: 0
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.fetchCommunityList()
  },
  methods: {
    fetchCommunityList() {
      getCommunityList().then(response => {
        this.communityList = response.data
        console.log(response.data)
      })
    },
    fetchData() {
      this.listLoading = true
      getList({ page: this.page, pageSize: this.pageSize, communityId: this.communityId, content: this.content }).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    sortById(a, b) {
      return a.id - b.id
    },
    handleSearch() {
      this.page = 1
      this.fetchData()
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.fetchData()
    },
    handleCurrentChange(page) {
      this.page = page
      this.fetchData()
    },
    handleDelete(rowId) {
      const ids = [rowId]
      this.$confirm('确定删除该记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRow(ids).then(() => {
          this.fetchData()
          this.$message.success('删除成功')
        })
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped>
.input-button-group {
  display: flex;
  align-items: center;
}

.input-button-group .el-input {
  flex: 1;
  margin-right: 10px;
}

.app-table {
  width: 100%;
}
</style>
